﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HalconDotNet;
using Project1.Common;
using Project1.Interface;

namespace Project1.Element
{
    public class Line : IElem
    {
        protected Point startPoint;
        protected Point endPoint;
        protected static int existCount = 0;
        protected double length;
        protected HObject lineContours,region;
        protected string name;

        public void Dispose()
        {
            if (lineContours != null) lineContours.Dispose();
            if (region != null) region.Dispose();
            lineContours = null;
            region = null;
        }

        public Line()
        {

        }
        public Line(Point startPoint,Point endPoint, string name,HObject lineContours)
        {
            this.lineContours = lineContours;
            HOperatorSet.GenRegionContourXld(lineContours, out region, "filled");
            HOperatorSet.DilationCircle(region, out region, 10);
            this.startPoint = new Point(startPoint);
            this.endPoint = new Point(endPoint);
            //this.drawingObjectID = drawingObjectID;
            this.name = name;
            this.length = startPoint.Distance(endPoint);
        }

        public Line(Point startPoint, Point endPoint)
        {
            double[] rows = { startPoint.Row,endPoint.Row };
            double[] cols = { startPoint.Column, endPoint.Column };
            HTuple hv_Rows = new HTuple(rows);
            HTuple hv_Columns = new HTuple(cols);
            HOperatorSet.GenContourPolygonXld(out this.lineContours, hv_Rows, hv_Columns);
            HOperatorSet.GenRegionContourXld(lineContours, out region, "filled");
            HOperatorSet.DilationCircle(region, out region, 10);
            this.startPoint = new Point(startPoint);
            this.endPoint = new Point(endPoint);
            //this.drawingObjectID = drawingObjectID;
            this.length = startPoint.Distance(endPoint);
        }
        public Line(Line line)
        {
            lineContours = new HObject(line.lineContours);
            if(line.region!=null)region = new HObject(line.region);

            startPoint = new Point(line.startPoint);
            endPoint = new Point(line.endPoint);
            //this.drawingObjectID = drawingObjectID;
            name = line.name;
            length = line.length;
     
        }


        public Line(double rowStart, double colStart, double rowEnd, double colEnd)
        {
            double[] rows = { rowStart, rowEnd };
            double[] cols = { colStart, colEnd };
            HTuple hv_Rows = new HTuple(rows);
            HTuple hv_Columns = new HTuple(cols);
            HOperatorSet.GenContourPolygonXld(out this.lineContours, hv_Rows, hv_Columns);
            HOperatorSet.GenRegionContourXld(lineContours, out region, "filled");
            HOperatorSet.DilationCircle(region, out region, 10);
            this.startPoint = new Point(rowStart, colStart, "default");
            this.endPoint = new Point(rowEnd, colEnd, "default");
            this.length = startPoint.Distance(endPoint);

        }

        public HObject LineContours 
        {
            get { return this.lineContours; }
            set { this.lineContours = value;}
        }
        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
            }
        }

        public Point StartPoint
        {
            get
            {
                return startPoint;
            }
            set
            {
                startPoint = new Point(value);
            }
        }

        public Point  EndPoint
        {
            get
            {
                return endPoint;
            }
            set
            {
                endPoint = new Point(value);
            }
        }

        public double Length
        {
            get
            {
                return length ;
            }
            set
            {
                length = value;
            }
        }

        public static Line FindItemByName(string name,List<Line>lines)
        {
            Line result =  null;
            foreach (var line in lines)
            {
                if(name == line.Name)
                {
                    result = new Line(line);
                    break;
                }
            }
            return result;
        }


        public override string ToString()
        {
            return "线:" + name + ",起点:" + startPoint + ",终点:" + endPoint+",长度:"+length.ToString("f1");
        }

        public void Reset(int num = 0)
        {
            existCount = 0;
        }

        public virtual void DispROI(HTuple windowHandle,string color)
        {
            
            HOperatorSet.SetColor(windowHandle, color);
            HOperatorSet.SetDraw(windowHandle, "margin");
            HObject roi = null;
            HOperatorSet.GenRegionContourXld(lineContours, out roi, "margin");
            HOperatorSet.DispObj(roi, windowHandle);
        }
        public void ApplyChange()
        {
            this.name = "LINE" + existCount++;
        }
        public HObject Region()
        {
            HOperatorSet.GenRegionContourXld(lineContours, out region, "filled");
            HOperatorSet.DilationCircle(region, out region, 5);
            return region;
        }



        public Point midPoint()
        {
            return new Point(0.5 * startPoint.Row + 0.5 * endPoint.Row, 0.5 * startPoint.Column + 0.5 * endPoint.Column);
        }

     
    }
}
